package de.goals.paulzerr;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;

public class OverviewActivity extends Activity {
	private TextView goalsEditText;

	private static final String URL_GOALS = "http://www.goals.paulzerr.de/android_php/goalsOverviewAndroid.php";

	ArrayList<HashMap<String, String>> goalsList;
	InputStream is;
	ArrayList<String> results = new ArrayList<String>();
	JSONObject json_data;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_overview_activity2);
		Bundle bundle = this.getIntent().getExtras();
		String userid = bundle.getString("userid");

		getData(userid);
	}

	public void getData(String userid) {
		Map<String,String> params = new HashMap<String,String>();
		params.put("userid", userid);
		@SuppressWarnings("unchecked")
		Map<String,String>[] parameters = new Map[4];
		parameters[0] = params;
		params.put("url", URL_GOALS);
		AsyncTask<Map<String, String>, Void, JSONObject> task = new GoalsPageConnector().execute(parameters);
		
		JSONObject jsonObject = null;
		try {
			jsonObject = task.get();
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		} catch (ExecutionException e1) {
			e1.printStackTrace();
		}
		try {

			int numberOfGoals = Integer.parseInt(jsonObject
					.get("numberOfGoals").toString());
			Log.i("Number of goals", ""+numberOfGoals);
			if (numberOfGoals == 0){
				ArrayList<String> al = new ArrayList<String>();
				al.add("<br/><b>You have no goals</b>");
				showData(al);
				return;
			}
			for (int i = 1; i <= numberOfGoals; i++) {

				String jo = jsonObject.get("" + i).toString();
				JSONObject jsonObjectGoalData = new JSONObject(jo);
				results.add("<br/>Goal: ");
				results.add("<b>"+jsonObjectGoalData.get("goalname_" + i).toString()+"</b>");
				results.add(" ");
				results.add(jsonObjectGoalData.get("subgoalscounter_" + i)
						.toString());
				results.add("<br/>");
				results.add("Description: ");
				results.add(jsonObjectGoalData.get("description_" + i)
						.toString());
				results.add("<br/>");
				results.add("Priority: ");
				results.add(jsonObjectGoalData.get("priority_" + i).toString());
				results.add("<br/>");
				results.add("Measuretype: ");
				results.add(jsonObjectGoalData.get("measuretype_" + i)
						.toString());
				results.add("<br/>");
				results.add("Measurename: ");
				results.add(jsonObjectGoalData.get("measurename_" + i)
						.toString());
				results.add("<br/>");
				results.add("Start value: ");
				results.add(jsonObjectGoalData.get("start_" + i).toString());
				results.add("<br/>");
				results.add("Current value: ");
				results.add(jsonObjectGoalData.get("current_" + i).toString());
				results.add("<br/>");
				results.add("Target value: ");
				results.add(jsonObjectGoalData.get("target_" + i).toString());
				results.add("<br/>");
				results.add("Goaldeadline: ");
				results.add(jsonObjectGoalData.get("goaldeadline_" + i)
						.toString());
				results.add("<br/><hr/>");

			}
			showData(results);
		} catch (JSONException e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
		}

	}

	public void showData(ArrayList<String> results) {
		goalsEditText = (TextView) findViewById(R.id.textView1);
		goalsEditText.setMovementMethod(new ScrollingMovementMethod());
		StringBuilder stb = new StringBuilder();
		for (String str : results) {
			stb.append(str);
		}
		goalsEditText.setText(Html.fromHtml(stb.toString()));
	}

}
